package org.adw.launcher2.ld.secret.ui.adapter;

import java.util.LinkedList;
import java.util.List;

import org.adw.launcher2.ld.secret.dom.MyPhoto;
import org.adw.launcher2.ld.secret.ui.utils.PhotoImageLoader;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import org.adw.launcher2.R;

public class MyPhotoAdapter extends ArrayAdapter<MyPhoto> {

	private Context context;
	private LayoutInflater inflater;
	public static List<MyPhoto> listToHide = null;
	public List<MyPhoto> listToThread = null;
	public PhotoImageLoader imageLoader; 
	public MyPhotoAdapter(Context context, int textViewResourceId,
			List<MyPhoto> list) {
		super(context, textViewResourceId, list);
		this.context = context;
		listToThread = list;
		inflater = LayoutInflater.from(context);
		listToHide = new LinkedList<MyPhoto>();
		
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;

			convertView = inflater.inflate(R.layout.my_photo_item, null);
			holder = new ViewHolder();
			holder.photoThumb = (ImageView) convertView.findViewById(R.id.imgPhoto);
			holder.photoName = (TextView) convertView.findViewById(R.id.txtPhotoName);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxPhoto);
			convertView.setTag(holder);
			
		final MyPhoto myPhoto = getItem(position);
		String [] temp = myPhoto.getAbsolutePathNoExtend().split("/");
		holder.photoName.setText(temp[temp.length-1]);
		if(myPhoto.image==null){
			imageLoader = new PhotoImageLoader(this.context,myPhoto, holder.photoThumb);
			imageLoader.execute();
		}else{
			holder.photoThumb.setImageBitmap(myPhoto.image);
		}
		holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					if(!listToHide.contains(myPhoto)){
						listToHide.add(myPhoto);
					}
				}else{
					listToHide.remove(myPhoto);
				}

			}
		});
		if(this.context.toString().contains("MyNonHiddenPhotoActivity")){
			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					showDialog(myPhoto.getId());
				}
			});
		}

		return convertView;
		
	}
	
	protected void showDialog(final long photoId) {
		final CharSequence[] items = {"View this photo"};

		AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
		Log.d("this.context", this.context.toString());
		builder.setTitle("Choose an action");
		builder.setItems(items, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
				Intent intent = new Intent
				(Intent.ACTION_VIEW, Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, ""+photoId));
				context.startActivity(intent);
		    }
		});	
		AlertDialog alert = builder.create();
		alert.show();
	}

	static class ViewHolder {
		ImageView photoThumb;
		TextView photoName;
		CheckBox checkBox;
		
	}
}
